//
//  NetworkService.swift
//  DyhlSdk
//
//  Created by wangbo on 2021/4/8.
//

import Foundation
import Moya
public enum NetworkService{
  
    
    case allRequest(json:String)
}
extension NetworkService:Moya.TargetType{
    public var baseURL: URL {
        let api = ApiKey.instance.BASE_URL
        return URL(string: api )!
    }
    
    public var path: String {
        switch self {
        case .allRequest:
            return "api/burying-point/event"
        }
    }
    
    public var method: Moya.Method {
        return .post
    }
    
    public var sampleData: Data {
        return "{}".data(using: String.Encoding.utf8)!
    }
    
    public var task: Task {
        switch self {
        case .allRequest(let data):
            return .requestData(data.utf8Encoded)
      
        }
    }
    
    public var headers: [String : String]? {
        switch self {
        case .allRequest:
           return ["Content-type":"application/json"]
        }
    }
    
    
}
public extension String {
    //日期 -> 字符串
    //字符串 -> 日期
    static let random_str_characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    static func randomStr(len : Int) -> String{
        var ranStr = ""
        for _ in 0..<len {
            let index = Int(arc4random_uniform(UInt32(random_str_characters.count)))
            ranStr.append(random_str_characters[random_str_characters.index(random_str_characters.startIndex, offsetBy: index)])
        }
        return ranStr
    }
    var IMGSLT:String{
        return self+"?x-oss-process=style/suoluetu_30"
    }
    var string2DateString : String {
        let formatter1 = DateFormatter()
        formatter1.locale = Locale.init(identifier: "en_US")
        formatter1.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSz"
        let date = formatter1.date(from: self) ?? Date()
        let formatter = DateFormatter()
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = "yyyy年MM月dd日 HH时:mm分:ss秒"
        return formatter.string(from: date)
    }
    var string2DateMMdd : String {
        let formatter1 = DateFormatter()
        formatter1.locale = Locale.init(identifier: "en_US")
        formatter1.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSz"
        let date = formatter1.date(from: self) ?? Date()
        let formatter = DateFormatter()
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = "MM月dd日 HH:mm:ss"
        return formatter.string(from: date)
    }
    var urlEscaped: String {
        return addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
    }
    
    var utf8Encoded: Data {
        return data(using: .utf8)!
    }
}
extension String {
//    func md5() -> String {
//        let str = self.cString(using: String.Encoding.utf8)
//        let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8))
//        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
//        let result = UnsafeMutablePointer<UInt8>.allocate(capacity: 32)
//        CC_MD5(str!, strLen, result)
//        let hash = NSMutableString()
//        for i in 0 ..< digestLen {
//            hash.appendFormat("%02x", result[i])
//        }
//        free(result)
//        return String(format: hash as String)
//    }
}


